package com.atstudy.string;

/**
 * @version v1.0
 * @description:
 * @author: admin
 * @date: 2022/12/20 10:28
 * @copyright: 博为峰
 */
public class StringDemo02 {
    public static void main(String[] args) {
//        m2();
//        m3();
//        m4();

    }

    private static void m4() {
    /*
       String s1 = "fasfkhsfkhsdfhsfhshkghakghdafkfhsdkhskfhs";
       String s2 = "fh";
       统计s2在s1中出现的次数
     */
        String s1 = "fasfkhsfkhsdfhsfhshkghakghdafkfhsdkhskfhs";
        String s2 = "fh";

        int count = 0;
        int index = 0;
       /* while ((index = s1.indexOf(s2)) != -1) {
            s1 = s1.substring(index + s2.length());
            count++;
        }*/

        while ((index = s1.indexOf(s2, index)) != -1) {
            index = index + s2.length();
            count++;
        }

        System.out.println("count = " + count);
    }

    private static void m3() {
        //转换
        String s = "张三,李四,王五";
        String s1 = s.concat(",赵六");//连接
//        System.out.println(s1);
        String s2 = s1.replace(",", ";");//替换
//        System.out.println(s2);
        String[] strings = s2.split(";");//拆分
    }

    private static void m2() {
        //判断
        String s1 = "abc"; //如果字符串常量池中没有，则创建对象
        String s2 = "cba";
        String s3 = "abc"; //如果字符串常量池中有内容相同的对象，则直接使用
        System.out.println(s1 == s2);//比较地址 false
        System.out.println(s1 == s3);//比较地址 true
        System.out.println(s1.equals(s2));  //比较内容
        System.out.println(s1.equals(s3));  //比较内容

        System.out.println(s1.contains("ab"));
        System.out.println(s1.startsWith("ab"));
        System.out.println(s1.endsWith("bc"));
        System.out.println(s1.equalsIgnoreCase("ABC"));//忽略大小写
    }

    public void m1() {
        //查
        String s = "abcdefghijklmnopqb";
        System.out.println(s.length());
        System.out.println(s.charAt(1));
        System.out.println(s.indexOf("b"));//返回指定字符在此字符串中第一次出现处的索引
        System.out.println(s.indexOf("b", 2));//找不到返回-1
        System.out.println(s.lastIndexOf("b"));
        System.out.println(s.substring(1));
        System.out.println(s.substring(1, 2));//包含开始，不包含结束
    }
}
